#include "stdafx.h"

SDLAdapter sdlAdapter;

Menu::Menu(SDLAdapter adapter){
	sdlAdapter = adapter;
}

void Menu::drawMenu(){

	SDL_Event event;
	int startGame = 0;
	int selection = 0;

	/* Enable Unicode translation */
	SDL_EnableUNICODE( 1 );

	loadMenuBitmaps();
	drawMenuBitmaps(selection);
	while( !startGame ){

		/* Poll for events */
		while( SDL_PollEvent( &event ) ){

			switch( event.type ){
				/* Keyboard event */
				/* Pass the event data onto PrintKeyInfo() */
					case SDL_KEYUP:
						switch(event.key.keysym.sym){
					case SDLK_w:
					case SDLK_UP:
						if(selection == 1) selection = 0;
						else selection = 1;
						drawMenuBitmaps(selection);
						break;
					case SDLK_s:
					case SDLK_DOWN:
						if(selection == 0) selection = 1;
						else selection = 0;
						drawMenuBitmaps(selection);
						break;
					case SDLK_ESCAPE:
						exit(0);
						break;
					case SDLK_RETURN:
						if(selection == 0){
							startGame = 1;
						}
						else exit(0);
						break;
						}
						break;


						/* SDL_QUIT event (window close) */
					case SDL_QUIT:
						exit(0);
						break;

					default:
						break;
			}

		}

	}

	sdlAdapter.startGame(title);

}

void Menu::loadMenuBitmaps(){
	loadtitle();
	loadNewgame();
	loadExit();
}

void Menu::drawMenuBitmaps(int selection){
	drawtitle();
	drawNewgame(selection);
	drawExit(selection);
	sdlAdapter.refreshScreen();
}


void Menu::drawtitle(){
	sdlAdapter.drawImage(title,50,50,441,52);
}

void Menu::drawNewgame(int selection){
	if(selection == 0) sdlAdapter.drawImage(newGameSelected,50,120,226,48);
	else sdlAdapter.drawImage(newGame,50,120,226,48);
}

void Menu::drawExit(int selection){
	if(selection == 1) sdlAdapter.drawImage(exitImageSelected,50,160,226,48);
	else sdlAdapter.drawImage(exitImage,50,160,226,48);
}


void Menu::loadtitle(){
	title = sdlAdapter.loadBitmap("images/menuTitle.bmp",50,50,441,52);
}

void Menu::loadNewgame(){
	newGameSelected = sdlAdapter.loadBitmap("images/newGameSelected.bmp",50,120,226,48);
	newGame = sdlAdapter.loadBitmap("images/newGame.bmp",50,120,226,48);
}

void Menu::loadExit(){
	exitImageSelected = sdlAdapter.loadBitmap("images/exitSelected.bmp",50,160,226,48);
	exitImage = sdlAdapter.loadBitmap("images/exit.bmp",50,160,226,48);
}